# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField


class AdminLogDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertAdminLog(self, info):
        """
        插入admin_log信息
        :param info:
        :return:
        """
        userId = info['userId']
        userName = info['userName']
        userIp = info['userIp']
        actionType = info['actionType']
        logRemark = info['logRemark']

        sql = "insert into zzh_admin_log (user_id,user_name,user_ip,action_type,log_remark) VALUES " \
              "(%s,%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId, userName, userId, actionType, logRemark])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findLogById(self, logId):
        """
        根据ID查询admin_log信息
        :param logId:
        :return:
        """
        sql = "select * from zzh_admin_log WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [logId])
        logJson = None
        if rs:
            list0 = cur.fetchall()
            json0 = Table2Json.admin_log_list_to_json(list0)
            logJson = json0
        cur.close()
        self.conn.close()
        return logJson

    def findLogByCondition(self, **kwargs):
        """
        根据条件查询admin_log信息
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        fieldMap = requestFieldMapToDBField.adminLogFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_admin_log WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_admin_log WHERE " + whereSentence
        rs = cur.execute(sql)

        logJson = None
        if rs:
            rsSet = cur.fetchall()
            list0 = MyPaging().getPage(rs, pageIndex, pageSize, rsSet)
            logJson = Table2Json.admin_log_list_to_json(list0)
        cur.close()
        self.conn.close()
        return logJson, rs

    def deleteAdminLog(self, idArray):
        """
        根据ID删除admin_log信息，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_admin_log WHERE id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateAdminLog(self, info):
        """
        更新admin_log
        :param info:
        :return:
        """
        logId = info['logId']
        userId = info['userId']
        userName = info['userName']
        userIp = info['userIp']
        actionType = info['actionType']
        logRemark = info['logRemark']

        sql = "update zzh_admin_log set user_id=%s,user_name=%s,user_ip=%s,action_type=%s,log_remark=%s " \
              " WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId, userName, userIp, actionType, logRemark, logId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectAdminLog(self, logId):
        """
        根据ID查询数据，返回元组
        :param logId:
        :return: data tuple
        """
        sql = "select * from zzh_admin_log WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [logId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr
